﻿using System;
using System.IO;
using System.Reflection;
using System.Web;

namespace Common
{
    /// <summary>
    ///     目录辅助函数
    /// </summary>
    public class DirectoryHelper
    {
        /// <summary>
        ///     是否存在目录
        /// </summary>
        /// <param name="sPath"></param>
        /// <returns></returns>
        public static bool Exits(string sPath)
        {
            return Directory.Exists(sPath);
        }

        /// <summary>
        ///     创建目录
        /// </summary>
        /// <param name="sPath"></param>
        /// <returns></returns>
        public static void CreateDirectory(string sPath)
        {
            try
            {
                if (!Directory.Exists(sPath))
                {
                    Directory.CreateDirectory(sPath);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        /// <summary>
        ///     删除目录
        /// </summary>
        /// <param name="sPath"></param>
        /// <param name="isDeleteRoot">是否删除根目录</param>
        public static void DeleteDirectory(string sPath, bool isDeleteRoot)
        {
            string[] strFiles = Directory.GetFiles(sPath);
            foreach (string strFile in strFiles)
            {
                try
                {
                    File.Delete(strFile);
                }
                catch
                {
                    LogHelper.Logger.Trace("Delete file error.");
                }
            }
            string[] strDirs = Directory.GetDirectories(sPath);
            foreach (string strDir in strDirs)
            {
                try
                {
                    DeleteDirectory(strDir, true);
                }
                catch
                {
                    LogHelper.Logger.Trace("Delete directory error.");
                }
            }

            if (isDeleteRoot)
            {
                //var f = new FileInfo(sPath) {Attributes = FileAttributes.Normal};
                Directory.Delete(sPath);
            }
        }

        /// <summary>
        ///     获取文件所在目录
        /// </summary>
        /// <param name="fullpath"></param>
        /// <returns></returns>
        public static string GetFilePath(string fullpath)
        {
            return Path.GetDirectoryName(fullpath);
        }

        /// <summary>
        ///     获取站点根路径
        /// </summary>
        /// <returns></returns>
        public static string GetWebBasePath()
        {
            return HttpContext.Current.Server.MapPath(@"~");
        }

        /// <summary>
        ///     获取Windows Service根路径
        /// </summary>
        /// <returns></returns>
        public static string GetServiceBasePath()
        {
            string myPath = Assembly.GetExecutingAssembly().Location;
            int i = myPath.LastIndexOf('\\');
            return myPath.Remove(i + 1);
        }

        /// <summary>
        ///     获得当前绝对路径含web和winform应用程序方法
        /// </summary>
        /// <returns></returns>
        public static string GetBasePath()
        {
            if (HttpContext.Current != null)
            {
                //如果是web
                return GetWebBasePath();
            }
            else //非web程序引用
            {
                return GetServiceBasePath();
            }
        }

        /// <summary>
        ///     获得当前绝对路径含web和winform应用程序方法
        /// </summary>
        /// <param name="strPath">指定的路径</param>
        /// <returns>绝对路径</returns>
        public static string GetMapPath(string strPath)
        {
            if (strPath.StartsWith("~/"))
                strPath = strPath.Substring(2);
            else if (strPath.StartsWith("/"))
                strPath = strPath.Substring(1);
            if (HttpContext.Current != null)
            {
                //如果是web 
                return Path.Combine(HttpContext.Current.Server.MapPath("~"), strPath);
            }
            else //非web程序引用
            {
                return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath);
            }
        }
    }
}